using System;
using System.Collections.Generic;
using System.Text;
using cde.UserControl;
using System.Web.UI;
using System.Data;
using Cit.CDE.Net.Module.Commons;
using Cit.CDE.Net.Module;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

[assembly: TagPrefix("UI.UserControl", "ui")]
namespace UI.UserControl
{
    public class SimpleSearchDG : SearchDataGrid
    {

        protected override void setColumns()
        {
            for (int i = 0; i < _size; i++)
            {
                BoundColumn bc = new BoundColumn();
                bc.DataField = "Lable_" + i;
                _dt.Columns.Add("Lable_" + i);
                //bc.ItemStyle.CssClass = "blueTH";
                bc.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
                this.Columns.Add(bc);

                TemplateColumn tc = new TemplateColumn();
                tc.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
                tc.ItemStyle.CssClass = "blue";
                tc.ItemTemplate = new CssConditonITemplate("Value_" + i);
                _dt.Columns.Add("Value_" + i, typeof(ConditionWithValue));
                this.Columns.Add(tc);
            }
        }

        protected override void setCondition()
        {
            int clmIndex = _size;
            DataRow dr = null;
            foreach (CDECondition condition in _em.ListConditions)
            {
                if (CDEConstants.ConstDispType_NO_DISP.Equals(condition.DISP_type)
                    || CDEConstants.ConstDispType_HIDDEN.Equals(condition.DISP_type))
                {
                    continue;
                }

                if (clmIndex == _size)
                {
                    clmIndex = 0;
                    dr = _dt.NewRow();
                    _dt.Rows.Add(dr);
                }

                string name = "txt_Value_" + clmIndex + "_" + condition.Name;
                string caption = condition.Label;
                
                if (CDEConstants.IsEmpty(condition.Aggregate)
                    && !CDEConstants.ConstDispType_READONLY.Equals(condition.DISP_type)
                    && !ConstDataType.DATE.Equals(condition.getP_Colmun().Type))
                {
                    //caption = "<a href=\"javascript:Show_SearchCandidate('"
                    //         + _em.Full_Name + "','" + _size + "','" + conditon.Name
                    //         + "','" + name + "')\">" + caption + "</a>";
                }
                else if (ConstDataType.DATE.Equals(condition.getP_Colmun().Type))
                {
                    caption = "<input type=\"hidden\" name=\"DATE_CLM\" id=\""
                        + name + "_date\" value=\"" + name + "\">"
                        + caption;
                }
                 
                dr[clmIndex * 2] = caption + ":";
                ConditionWithValue cv = new ConditionWithValue(_em, condition);
                cv.Value = _cndvalue[condition.Name];

                dr[clmIndex * 2 + 1] = cv;
                clmIndex++;
            }

            this.DataSource = _dt;
        }

        public static void setScFromRequest(CDEEntityManager em, int clmSize, CDESearchCriteria sc, NameValueCollection request)
        {
            int clmIndex = clmSize;
            foreach (CDECondition condition in em.ListConditions)
            {
                if (CDEConstants.ConstDispType_NO_DISP.Equals(condition.DISP_type)
                    || CDEConstants.ConstDispType_HIDDEN.Equals(condition.DISP_type))
                {
                    continue;
                }

                if (clmIndex == clmSize)
                {
                    clmIndex = 0;
                }

                //if (ConstDataType.DATE.Equals(condition.getP_Colmun().Type) && em.getEMGName() == CDEConstants.CONST_EMG)
                //{
                //    CssConditonITemplate.setDateScFromRequest("Value_", clmIndex, condition, request, sc);
                //    clmIndex++;
                //    clmIndex++;
                //}
                //else
                //{
                    CssConditonITemplate.setScFromRequest("Value_" + clmIndex, condition, request, sc);
                    clmIndex++;
                //}
            }
        }

        public override void setScFromRequest(CDESearchCriteria sc, NameValueCollection request)
        {
            setScFromRequest(_em, _size, sc, request);
        }
    }
}
